/**
 *	Contains 'Rules' / 'Win conditions' for the '3 musketeers' game type
**/

"use strict"


/**
 *	Add game rules
**/
GameLogic.validator.addRules({
	
	// a combination of two rules. no branching logic, so this will have to do for now
	player1AndDestinationSquareContainsOpponent: function(game, move) {
		if ( game.turn !== 1 ) {
			// this is a 'success', since it's not the current players turn, this rule does not apply
			return { success: true }
		}
		
		return this.checkDestinationSquareContainsOpponent(game, move)
	},
	
	
	// another combination of two rules, as above
	player2AndDestinationSquareEmpty: function(game, move) {
		if ( game.turn !== 2 ) {
			return { success: true }
		}
		
		// defined in game 'SWAP'
		return this.checkDestinationSquareEmpty(game, move)
	},
	
	
	// returns true if the player is 'taking' his opponent
	checkDestinationSquareContainsOpponent: function(game, move) {
		// figure out who the opponent is
		var opponent = ( game.turn === 1 ? 2 : 1 )
		
		if ( game.state[move.y2][move.x2] === opponent ) {
			return { success: true }
		}
		
		return {
			success: false,
			message: "can't move, destination square does not contain your opponent"
		}
	}
	
})



/**
 *	Add win conditions
**/
GameLogic.winChecker.addRules({
		
	// player 1 loses if at any point he moves his piece into a single row.
	// this is checked before the win condition below.
	// note: only evaluates for 3 pieces at the moment
	player1InOneRowLose: function(game) {
	
		// get the positions of all player 1 pieces
		var p1 = [ ]
		for (var y=0; y<game.board.h; y++) {
			for (var x=0; x<game.board.w; x++) {
				if ( game.state[y][x] === 1 ) {
					p1.push({ x: x, y: y })
				}
			}
		}
		
		// if the x or y positions of all three are the same, player 2 wins
		if ( p1[0].x === p1[1].x && p1[0].x === p1[2].x ) {
			return 2
		} else if ( p1[0].y === p1[1].y && p1[0].y === p1[2].y ) {
			return 2
		}
		
		// since neither is true there must be no winner yet
		return 0
	},
	
	
	// player 1 wins if it is his turn and he can't take his opponent!
	player1CannotTakeOpponentWin: function(game) {
		// can't win on player 2's go
		if ( game.turn !== 1 )  return 0
		
		// get the positions of all player 1 pieces
		var p1 = [ ]
		for (var y=0; y<game.board.h; y++) {
			for (var x=0; x<game.board.w; x++) {
				if ( game.state[y][x] === 1 ) {
					p1.push({ x: x, y: y })
				}
			}
		}
		
		// 'test move' helper function - returns true if the player can make a move in the direction specified
		var testm = function(piece, xoff, yoff) {
			var newx = piece.x + xoff,
				newy = piece.y + yoff
			
			// check if the pieces are off the board	
			if ( newx < 0 || newx >= game.board.w )  return false
			if ( newy < 0 || newy >= game.board.h )  return false
			
			// check if the opponents piece exists
			if ( game.state[newy][newx] === 2 ) {
				return true
			} else {
				return false
			}
		}
		
		// check all 4 directions for each piece. if any are true (contain player 2), the game can continue
		for (var i=0; i<p1.length; i++) {
			if (testm(p1[i], 0, -1) ||			//up
				testm(p1[i], 1, 0 ) ||			//right
				testm(p1[i], 0, 1 ) ||			//down
				testm(p1[i], -1, 0)				//left
			) {
				return 0
			}
		}
		
		// if we reach here, player 1 must have won!
		return 1
	}

})
